<?php

class Track{
	public $Id;
	public $Nombre;
	public $track_chair_id;
	
	function __construct($id, $track, $track_chair_id){
		$this->Id = $id;
		$this->Nombre = $track;
		$this->track_chair_id = $track_chair_id;
	}
	
	public function Insertar(){
		global $dbh;
		$sql = 'INSERT INTO  `tracks` (
				`track_id` ,
				`track`
				)
				VALUES (
				NULL ,
				\''.$this->track.'\'
				)';
				
		$temp_q_id = $dbh->prepare($sql);
		if (!$temp_q_id) {
				echo "\nPDO::errorInfo():\n";		
		}
		$temp_q_id->execute();
					
		$this->id = $dbh->lastInsertId();		
	}
	
	static function Listar(){
		global $dbh;
		$dbh->query("SET NAMES 'utf8'");
		$sql = 'SELECT track_id, track, track_chair_id FROM tracks';
		$lista = array();
		 
		foreach($dbh->query($sql) as $row) {
			$lista[] = new Track($row['track_id'], $row['track'], $row['track_chair_id']);
		}
		
		return $lista;
	}
	
	static function Buscar($texto){
		global $dbh;
		
		$sql = 'SELECT `track` FROM `tracks` WHERE track LIKE "'.$texto.'"';
		$resultado = $dbh->query($sql);
		
		if($resultado->rowCount() > 0){
			return true;	
		}
		else{
			return false;	
		}
		
	}
	
	static function Actualizar($id,$track){
		global $dbh;
		
		$sql = 'UPDATE `tracks` SET
				`track`=\''.$track.'\'  
				WHERE 
				`track_id`=\''.$id.'\' ';
		
		$temp_q_id = $dbh->prepare($sql);
		if (!$temp_q_id) {
				echo "\nPDO::errorInfo():\n";		
		}
		$temp_q_id->execute();
		
	}
	
	static function Eliminar($id)
	{
		global $dbh;
		
		$sql = "Delete from tracks where track_id = ".$id;
		$temp_q_id = $dbh->prepare($sql);
		if (!$temp_q_id) {
			echo "\nPDO::errorInfo():\n";				
		}
		$temp_q_id->execute();		
	}
	
	static function Obtener($id){
		global $dbh;
		
		$sql = 'SELECT `track_id`,`track`,`track_chair_id` FROM `tracks` WHERE track_id = '.$id;
		$track = NULL;
			
		foreach($dbh->query($sql) as $row){
			$track = new Track($row['track_id'],$row['track'],$row['track_chair_id']);		
		}	
		
		return $track;
	}
	
	static function ListarChairsPorTrack($id){
		global $dbh;
		
		$sql = 'SELECT u.`idusuario` FROM `usuario` u 
		inner join `usuario_rol` ur 
		on ur.idusuario = u.idusuario
		WHERE idtrack = '.$id.' and ur.idrol = 6';
		
		$lista = array();
		foreach($dbh->query($sql) as $row){
			$lista[] = Usuario::Obtener($row["idusuario"]);	
		}	
		
		return $lista;
	}
	
}
?>